using SM;
using SS;
using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
namespace TVSBH.func
{
	public class frmSelectProduct : Form
	{
		private Func objFunc = new Func();
		public string productID = "";
		public string productName = "";
		public string priceBase = "";
		public string priceTrade = "";
		public string priceRetail = "";
		public string unitName = "";
		public string nationalName = "";
		public string productPossName = "";
		private bool Loading = false;
		public bool isInput = false;
		public bool sellWithBill;
		private IContainer components = null;
		private Grid dgvProducts;
		private GroupBox groupBox1;
		private ComboBox cbbGroup;
		private Button btnClose;
		private DataGridViewTextBoxColumn possID;
		private DataGridViewTextBoxColumn possName;
		private DataGridViewCheckBoxColumn dg1;
		private DataGridViewTextBoxColumn dg2;
		private DataGridViewTextBoxColumn Column2;
		private DataGridViewTextBoxColumn Column1;
		private DataGridViewTextBoxColumn dg6;
		private DataGridViewTextBoxColumn Column3;
		private DataGridViewTextBoxColumn Column4;
		private DataGridViewTextBoxColumn Column5;
		private DataGridViewTextBoxColumn P_possName;
		private Label lblNumberItem;
		public TextBox txtSearch;
		private Label label1;
		public frmSelectProduct()
		{
			this.InitializeComponent();
			this.txtSearch.Focus();
		}
		private void frmSelectProduct_Load(object sender, EventArgs e)
		{
			this.objFunc.LoadComboBoxGProduct("SELECT GpID, UPPER(GpName) AS GpName FROM TabGproducts ORDER BY GpName", this.cbbGroup, "GpID", "GpName");
			this.getProduct(this.cbbGroup.SelectedValue.ToString(), this.txtSearch.Text);
			this.Loading = true;
			this.txtSearch.Focus();
		}
		private void btnSelect_Click(object sender, EventArgs e)
		{
			base.Close();
		}
		private void getProduct(string gid, string pName)
		{
			string text;
			if (gid != "0")
			{
				text = " AND GpID = " + gid + " ";
			}
			else
			{
				text = " ";
			}
			string text2;
			if (pName != "")
			{
				text2 = string.Concat(new string[]
				{
					" AND (Upper(TabProducts.P_Name) LIKE N'%",
					pName.ToUpper(),
					"%' OR Upper(TabProducts.NameSearch) LIKE N'%",
					pName.ToUpper(),
					"%' ) "
				});
			}
			else
			{
				text2 = " ";
			}
			string text3;
			if (this.sellWithBill)
			{
				text3 = " AND [TabProducts].[SellWithBill] = 1 ";
			}
			else
			{
				text3 = " ";
			}
			this.dgvProducts.AutoGenerateColumns = false;
			SqlConnection sqlConnection = new SqlConnection(this.objFunc.strConnectionString);
			string cmdText;
			if (this.isInput)
			{
				cmdText = "SELECT TOP 100 PERCENT 0 AS TT, PID, UPPER(P_Name) AS P_Name, P_PriceBase, P_PriceTrade, P_PriceRetail, UnitName, possName FROM [TabProducts],[TabUnitProduct] WHERE [TabProducts].[P_DonViTinh] = [TabUnitProduct].[UnitID] " + text2 + text + " ORDER BY [P_Name]";
			}
			else
			{
				cmdText = string.Concat(new string[]
				{
					"SELECT TOP 100 PERCENT 0 AS TT, PID, UPPER(P_Name) AS P_Name, P_PriceBase, P_PriceTrade, P_PriceRetail, UnitName, possName FROM [TabProducts],[TabUnitProduct] WHERE [TabProducts].[P_DonViTinh] = [TabUnitProduct].[UnitID] ",
					text2,
					text,
					text3,
					" AND PID IN (SELECT PID FROM TabTotalBalancer WHERE productNumberOut < productNumberIn AND productNumberIn > 0) ORDER BY [P_Name]"
				});
			}
			SqlCommand selectCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(selectCommand);
			DataSet dataSet = new DataSet();
			sqlConnection.Open();
			sqlDataAdapter.Fill(dataSet);
			this.dgvProducts.DataSource = dataSet.Tables[0];
			this.lblNumberItem.Text = "Tìm thấy " + this.dgvProducts.Rows.Count.ToString() + " sản phẩm";
			sqlConnection.Close();
			sqlConnection.Dispose();
			sqlDataAdapter.Dispose();
			dataSet.Dispose();
		}
		private void frmSelectProduct_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
			else
			{
				if (e.KeyCode == Keys.F3)
				{
					this.txtSearch.Focus();
					this.txtSearch.SelectionStart = this.txtSearch.Text.Trim().Length;
				}
			}
		}
		private void btnClose_Click(object sender, EventArgs e)
		{
			this.productName = "";
			this.unitName = "";
			this.priceBase = "";
			this.priceTrade = "";
			this.priceRetail = "";
			this.nationalName = "";
			this.productID = "";
			base.Close();
		}
		private void dgvProducts_CellEnter(object sender, DataGridViewCellEventArgs e)
		{
			try
			{
				this.productName = this.dgvProducts.Rows[e.RowIndex].Cells[1].Value.ToString();
				this.unitName = this.dgvProducts.Rows[e.RowIndex].Cells[2].Value.ToString();
				this.priceBase = this.dgvProducts.Rows[e.RowIndex].Cells[5].Value.ToString();
				this.priceTrade = this.dgvProducts.Rows[e.RowIndex].Cells[6].Value.ToString();
				this.priceRetail = this.dgvProducts.Rows[e.RowIndex].Cells[7].Value.ToString();
				this.nationalName = this.dgvProducts.Rows[e.RowIndex].Cells[3].Value.ToString();
				this.productID = this.dgvProducts.Rows[e.RowIndex].Cells[4].Value.ToString();
				this.productPossName = this.dgvProducts.Rows[e.RowIndex].Cells["P_possName"].Value.ToString();
			}
			catch
			{
			}
		}
		private void dgvProducts_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			base.Close();
		}
		public void dgvProducts_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\r')
			{
				base.Close();
			}
		}
		private void cbbGroup_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.Loading)
			{
				this.getProduct(this.cbbGroup.SelectedValue.ToString(), this.txtSearch.Text);
			}
		}
		private void txtSearch_TextChanged(object sender, EventArgs e)
		{
			if (this.Loading)
			{
				this.getProduct(this.cbbGroup.SelectedValue.ToString(), this.txtSearch.Text);
			}
		}
		private void txtSearch_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Down)
			{
				this.dgvProducts.Focus();
			}
		}
		private void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\r')
			{
				this.dgvProducts.Focus();
			}
		}
		private void txtSearch_Enter(object sender, EventArgs e)
		{
			this.txtSearch.BackColor = Color.Aqua;
		}
		private void txtSearch_Leave(object sender, EventArgs e)
		{
			this.txtSearch.BackColor = Color.White;
		}
		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}
		private void InitializeComponent()
		{
			DataGridViewCellStyle dataGridViewCellStyle = new DataGridViewCellStyle();
			DataGridViewCellStyle dataGridViewCellStyle2 = new DataGridViewCellStyle();
			DataGridViewCellStyle dataGridViewCellStyle3 = new DataGridViewCellStyle();
			DataGridViewCellStyle dataGridViewCellStyle4 = new DataGridViewCellStyle();
			DataGridViewCellStyle dataGridViewCellStyle5 = new DataGridViewCellStyle();
			DataGridViewCellStyle dataGridViewCellStyle6 = new DataGridViewCellStyle();
			ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof(frmSelectProduct));
			this.groupBox1 = new GroupBox();
			this.txtSearch = new TextBox();
			this.cbbGroup = new ComboBox();
			this.btnClose = new Button();
			this.lblNumberItem = new Label();
			this.label1 = new Label();
			this.dgvProducts = new Grid();
			this.dg1 = new DataGridViewCheckBoxColumn();
			this.dg2 = new DataGridViewTextBoxColumn();
			this.Column2 = new DataGridViewTextBoxColumn();
			this.Column1 = new DataGridViewTextBoxColumn();
			this.dg6 = new DataGridViewTextBoxColumn();
			this.Column3 = new DataGridViewTextBoxColumn();
			this.Column4 = new DataGridViewTextBoxColumn();
			this.Column5 = new DataGridViewTextBoxColumn();
			this.P_possName = new DataGridViewTextBoxColumn();
			this.groupBox1.SuspendLayout();
			((ISupportInitialize)this.dgvProducts).BeginInit();
			base.SuspendLayout();
			this.groupBox1.Controls.Add(this.txtSearch);
			this.groupBox1.Controls.Add(this.cbbGroup);
			this.groupBox1.Location = new Point(7, 1);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new Size(485, 50);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Nhóm sản phẩm - Tên sản phẩm";
			this.txtSearch.AcceptsReturn = true;
			this.txtSearch.Font = new Font("Arial", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
			this.txtSearch.Location = new Point(218, 19);
			this.txtSearch.Name = "txtSearch";
			this.txtSearch.Size = new Size(258, 22);
			this.txtSearch.TabIndex = 1;
			this.txtSearch.TextChanged += new EventHandler(this.txtSearch_TextChanged);
			this.txtSearch.KeyDown += new KeyEventHandler(this.txtSearch_KeyDown);
			this.txtSearch.Leave += new EventHandler(this.txtSearch_Leave);
			this.txtSearch.KeyPress += new KeyPressEventHandler(this.txtSearch_KeyPress);
			this.txtSearch.Enter += new EventHandler(this.txtSearch_Enter);
			this.cbbGroup.BackColor = Color.White;
			this.cbbGroup.DropDownStyle = ComboBoxStyle.DropDownList;
			this.cbbGroup.FlatStyle = FlatStyle.Flat;
			this.cbbGroup.FormattingEnabled = true;
			this.cbbGroup.Location = new Point(12, 18);
			this.cbbGroup.Name = "cbbGroup";
			this.cbbGroup.Size = new Size(200, 23);
			this.cbbGroup.TabIndex = 4;
			this.cbbGroup.SelectedIndexChanged += new EventHandler(this.cbbGroup_SelectedIndexChanged);
			this.btnClose.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);
			this.btnClose.BackColor = SystemColors.ButtonFace;
			this.btnClose.FlatAppearance.BorderColor = Color.FromArgb(255, 192, 255);
			this.btnClose.FlatStyle = FlatStyle.Flat;
			this.btnClose.Font = new Font("Arial", 9f, FontStyle.Bold, GraphicsUnit.Point, 163);
			this.btnClose.Location = new Point(392, 260);
			this.btnClose.Name = "btnClose";
			this.btnClose.Size = new Size(96, 28);
			this.btnClose.TabIndex = 5;
			this.btnClose.Text = "Thoát  [ ESC ]";
			this.btnClose.UseVisualStyleBackColor = false;
			this.btnClose.Click += new EventHandler(this.btnClose_Click);
			this.lblNumberItem.Anchor = (AnchorStyles.Bottom | AnchorStyles.Left);
			this.lblNumberItem.BackColor = Color.White;
			this.lblNumberItem.Location = new Point(10, 262);
			this.lblNumberItem.Name = "lblNumberItem";
			this.lblNumberItem.Size = new Size(380, 25);
			this.lblNumberItem.TabIndex = 24;
			this.lblNumberItem.Text = "-:-";
			this.lblNumberItem.TextAlign = ContentAlignment.MiddleLeft;
			this.label1.Anchor = (AnchorStyles.Bottom | AnchorStyles.Left);
			this.label1.BackColor = Color.White;
			this.label1.Location = new Point(7, 256);
			this.label1.Name = "label1";
			this.label1.Size = new Size(485, 36);
			this.label1.TabIndex = 25;
			this.label1.TextAlign = ContentAlignment.MiddleLeft;
			this.dgvProducts.AllowUserToAddRows = false;
			this.dgvProducts.AllowUserToDeleteRows = false;
			this.dgvProducts.AllowUserToResizeColumns = false;
			this.dgvProducts.AllowUserToResizeRows = false;
			dataGridViewCellStyle.BackColor = Color.FromArgb(192, 255, 255);
			dataGridViewCellStyle.Font = new Font("Arial", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
			this.dgvProducts.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle;
			this.dgvProducts.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right);
			this.dgvProducts.BackgroundColor = Color.White;
			this.dgvProducts.BorderStyle = BorderStyle.Fixed3D;
			this.dgvProducts.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
			dataGridViewCellStyle2.BackColor = SystemColors.Control;
			dataGridViewCellStyle2.Font = new Font("Arial", 9f, FontStyle.Regular, GraphicsUnit.Point, 0);
			dataGridViewCellStyle2.ForeColor = SystemColors.WindowText;
			dataGridViewCellStyle2.SelectionBackColor = Color.Aqua;
			dataGridViewCellStyle2.SelectionForeColor = Color.Black;
			this.dgvProducts.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle2;
			this.dgvProducts.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
			this.dgvProducts.Columns.AddRange(new DataGridViewColumn[]
			{
				this.dg1,
				this.dg2,
				this.Column2,
				this.Column1,
				this.dg6,
				this.Column3,
				this.Column4,
				this.Column5,
				this.P_possName
			});
			this.dgvProducts.Cursor = Cursors.Hand;
			this.dgvProducts.EnableHeadersVisualStyles = false;
			this.dgvProducts.Location = new Point(7, 53);
			this.dgvProducts.MultiSelect = false;
			this.dgvProducts.Name = "dgvProducts";
			this.dgvProducts.ReadOnly = true;
			dataGridViewCellStyle3.BackColor = SystemColors.Control;
			dataGridViewCellStyle3.Font = new Font("Arial", 9f, FontStyle.Regular, GraphicsUnit.Point, 0);
			dataGridViewCellStyle3.ForeColor = Color.Black;
			dataGridViewCellStyle3.SelectionBackColor = Color.Aqua;
			dataGridViewCellStyle3.SelectionForeColor = Color.Black;
			this.dgvProducts.RowHeadersDefaultCellStyle = dataGridViewCellStyle3;
			this.dgvProducts.RowHeadersVisible = false;
			this.dgvProducts.RowHeadersWidth = 26;
			this.dgvProducts.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
			dataGridViewCellStyle4.Font = new Font("Arial", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
			this.dgvProducts.RowsDefaultCellStyle = dataGridViewCellStyle4;
			this.dgvProducts.RowTemplate.DefaultCellStyle.BackColor = Color.White;
			this.dgvProducts.RowTemplate.DefaultCellStyle.Font = new Font("Arial", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
			this.dgvProducts.RowTemplate.DefaultCellStyle.ForeColor = Color.Black;
			this.dgvProducts.RowTemplate.DefaultCellStyle.SelectionBackColor = Color.Aqua;
			this.dgvProducts.RowTemplate.DefaultCellStyle.SelectionForeColor = Color.Black;
			this.dgvProducts.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
			this.dgvProducts.Size = new Size(485, 198);
			this.dgvProducts.TabIndex = 2;
			this.dgvProducts.CellDoubleClick += new DataGridViewCellEventHandler(this.dgvProducts_CellDoubleClick);
			this.dgvProducts.CellEnter += new DataGridViewCellEventHandler(this.dgvProducts_CellEnter);
			this.dgvProducts.KeyPress += new KeyPressEventHandler(this.dgvProducts_KeyPress);
			this.dg1.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
			this.dg1.DataPropertyName = "TT";
			dataGridViewCellStyle5.Alignment = DataGridViewContentAlignment.MiddleCenter;
			dataGridViewCellStyle5.NullValue = "False";
			this.dg1.DefaultCellStyle = dataGridViewCellStyle5;
			this.dg1.FalseValue = "0";
			this.dg1.HeaderText = "";
			this.dg1.IndeterminateValue = "0";
			this.dg1.Name = "dg1";
			this.dg1.ReadOnly = true;
			this.dg1.Resizable = DataGridViewTriState.False;
			this.dg1.TrueValue = "1";
			this.dg1.Width = 30;
			this.dg2.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
			this.dg2.DataPropertyName = "P_Name";
			dataGridViewCellStyle6.Alignment = DataGridViewContentAlignment.MiddleLeft;
			dataGridViewCellStyle6.Font = new Font("Arial", 9f, FontStyle.Regular, GraphicsUnit.Point, 0);
			dataGridViewCellStyle6.NullValue = null;
			this.dg2.DefaultCellStyle = dataGridViewCellStyle6;
			this.dg2.HeaderText = "Tên sản phẩm";
			this.dg2.MinimumWidth = 120;
			this.dg2.Name = "dg2";
			this.dg2.ReadOnly = true;
			this.Column2.DataPropertyName = "UnitName";
			this.Column2.HeaderText = "Đơn vị tính";
			this.Column2.Name = "Column2";
			this.Column2.ReadOnly = true;
			this.Column1.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
			this.Column1.DataPropertyName = "UnitName";
			this.Column1.HeaderText = "Đơn vị tính";
			this.Column1.MinimumWidth = 160;
			this.Column1.Name = "Column1";
			this.Column1.ReadOnly = true;
			this.Column1.Visible = false;
			this.Column1.Width = 160;
			this.dg6.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
			this.dg6.DataPropertyName = "PID";
			this.dg6.HeaderText = "PID";
			this.dg6.Name = "dg6";
			this.dg6.ReadOnly = true;
			this.dg6.Resizable = DataGridViewTriState.False;
			this.dg6.Visible = false;
			this.dg6.Width = 5;
			this.Column3.DataPropertyName = "P_PriceBase";
			this.Column3.HeaderText = "Giá nhập";
			this.Column3.Name = "Column3";
			this.Column3.ReadOnly = true;
			this.Column3.Visible = false;
			this.Column4.DataPropertyName = "P_PriceTrade";
			this.Column4.HeaderText = "Giá bán buôn";
			this.Column4.Name = "Column4";
			this.Column4.ReadOnly = true;
			this.Column4.Visible = false;
			this.Column5.DataPropertyName = "P_PriceRetail";
			this.Column5.HeaderText = "Giá bán lẻ";
			this.Column5.Name = "Column5";
			this.Column5.ReadOnly = true;
			this.Column5.Visible = false;
			this.P_possName.DataPropertyName = "possName";
			this.P_possName.HeaderText = "possName";
			this.P_possName.Name = "P_possName";
			this.P_possName.ReadOnly = true;
			this.P_possName.Visible = false;
			base.AutoScaleDimensions = new SizeF(7f, 15f);
			//base.AutoScaleMode = AutoScaleMode.Font;
			base.ClientSize = new Size(499, 300);
			base.Controls.Add(this.lblNumberItem);
			base.Controls.Add(this.btnClose);
			base.Controls.Add(this.groupBox1);
			base.Controls.Add(this.dgvProducts);
			base.Controls.Add(this.label1);
			this.Cursor = Cursors.Hand;
			this.Font = new Font("Arial", 9f, FontStyle.Regular, GraphicsUnit.Point, 0);
			//base.Icon = (System.Drawing.Icon)componentResourceManager.GetObject("$this.Icon");
			//base.Icon = (Icon)componentResourceManager.GetObject("$this.Icon");
			base.KeyPreview = true;
			base.Name = "frmSelectProduct";
			base.ShowInTaskbar = false;
			base.StartPosition = FormStartPosition.Manual;
			this.Text = "..: Lựa chọn sản phẩm";
			base.Load += new EventHandler(this.frmSelectProduct_Load);
			base.KeyDown += new KeyEventHandler(this.frmSelectProduct_KeyDown);
			this.groupBox1.ResumeLayout(false);
			this.groupBox1.PerformLayout();
			((ISupportInitialize)this.dgvProducts).EndInit();
			base.ResumeLayout(false);
		}
	}
}
